//
//  BiZoomScrollView.m
//  XinBanEducation
//
//  Created by bindu on 14-6-6.
//  Copyright (c) 2014年 Nahan Cop. All rights reserved.
//

#import "BiZoomScrollView.h"

@interface BiZoomScrollView()<UIScrollViewDelegate>
@property (nonatomic, retain) UIImageView *imageView;
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center;
@end

@implementation BiZoomScrollView
@synthesize imageView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        self.backgroundColor = [UIColor blackColor];
        self.delegate = self;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        [self initImageView];
    }
    return self;
}

- (void)setImage:(UIImage*)image
{
    self.imageView.image = image;
    //self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    
    self.imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
    float minimumScale = self.frame.size.width / imageView.frame.size.width;
    
    minimumScale = 1.0;
    
    [self setMinimumZoomScale:minimumScale];
    [self setZoomScale:1];
    [self setMaximumZoomScale:2.0];
    
    /// 居中
    CGFloat offsetX = (self.bounds.size.width > self.contentSize.width)?(self.bounds.size.width - self.contentSize.width)/2 : 0.0;
    CGFloat offsetY = (self.bounds.size.height > self.contentSize.height)?(self.bounds.size.height - self.contentSize.height)/2 : 0.0;
    self.imageView.center = CGPointMake(self.contentSize.width/2 + offsetX,self.contentSize.height/2 + offsetY);
}

- (void)initImageView
{
    UIImageView * tmpImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width * 2.5, self.frame.size.height * 2.5)];
    tmpImageView.userInteractionEnabled = YES;
    [self addSubview:tmpImageView];
    self.imageView = tmpImageView;
    
    // Add gesture,double tap zoom imageView.
    UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                       action:@selector(handleDoubleTap:)];
    [doubleTapGesture setNumberOfTapsRequired:2];
    [self.imageView addGestureRecognizer:doubleTapGesture];

}

#pragma mark - Zoom methods
- (void)handleDoubleTap:(UIGestureRecognizer *)gesture
{
    float newScale = self.zoomScale * 1.5;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gesture locationInView:gesture.view]];
    [self zoomToRect:zoomRect animated:YES];
}

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center
{
    CGRect zoomRect;
    zoomRect.size.height = self.imageView.frame.size.height / scale;
    zoomRect.size.width  = self.imageView.frame.size.width  / scale;
    zoomRect.origin.x = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
    return zoomRect;
}

#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    /// 居中
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
    self.imageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
//    float minimumScale = self.frame.size.width / imageView.image.size.width;
//    if(scale == minimumScale)
//    {
//        self.imageView.frame = CGRectMake(0, 30, self.imageView.frame.size.width, self.imageView.frame.size.height);
//    }
//    else
//    {
//        self.imageView.frame = CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height);
        [scrollView setZoomScale:scale animated:NO];
//    }
}

#pragma mark - View cycle
- (void)dealloc
{
    self.imageView = nil;
   
}


@end
